package com.format.Controller;

import com.format.Public.MusicFormat;
import com.format.Service.MusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
public class MusicController {
    @Autowired
    List<MusicService> formatList;

    @PostMapping(value = "/home/upload/music")
    public String uploadMusic(@RequestPart("file") MultipartFile file, HttpServletResponse response) throws Exception{
        String fileName = file.getOriginalFilename();
        String[] split = fileName.split("\\.");
        if (split != null){
            List<MusicService> list = formatList.stream().
                    filter(service -> split[split.length-1].
                            equals(service.getType().getName())).toList();
            if (list.size() == 1){
                list.get(0).decode(file,response);
            }else {

            }
        }

        return "ok";
    }
}
